use input_method::core::dag_params::Word;
use serde_json;
use std::{collections::HashMap, fs};

pub fn load_json<T: serde::de::DeserializeOwned>(path: &str) -> T {
    let data = fs::read_to_string(path).expect(format!("Unable to read file: {}", path).as_str());
    serde_json::from_str(&data)
        .expect(format!("JSON was not well-formatted in file: {}", path).as_str())
}

// 加载字典文件
pub fn load_dict(file_path: &str) -> HashMap<String, Vec<Word>> {
    load_json::<HashMap<String, Vec<(String, f64)>>>(file_path)
        .into_iter()
        .map(|(k, v)| {
            (
                k,
                v.into_iter()
                    .map(|(word, score)| Word::new(word, score))
                    .collect(),
            )
        })
        .collect()
}
